home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / zoomrecter / zoomrecter.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  5.1 KB  |  222 lines

  1. /*
  2.     File:        ZoomRecter.c
  3.  
  4.     Contains:    This snippet shows how to do "Finder" style zooming between two rectangles.
  5.                 The boolean flag "kZoomLarger" controls the proportional direction of the
  6.                 zooming.
  7.     
  8.                 To get the two rectangles, you drag them out rubberbanded, and the zoom
  9.                 occurs between them.  To quit, click the close box.
  10.     
  11.                 If you want to do zooms between windows, open up a port with the dimensions
  12.                 of the desktop (from GetGrayRgn()).
  13.     
  14.                 DON'T use this as a sample of how to do rubberband drawing!!!  It's sort of
  15.                 hacked together bypassing the event mechanism and just using Button().
  16.  
  17.     Written by: Steve Falkenburg    
  18.  
  19.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  20.  
  21.                 You may incorporate this Apple sample source code into your program(s) without
  22.                 restriction. This Apple sample source code has been provided "AS IS" and the
  23.                 responsibility for its operation is yours. You are not permitted to redistribute
  24.                 this Apple sample source code as "Apple sample source code" after having made
  25.                 changes. If you're going to re-distribute the source, we require that you make
  26.                 it clear in the source that the code was descended from Apple sample source
  27.                 code, but that you've made changes.
  28.  
  29.     Change History (most recent first):
  30.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  31.                 
  32.  
  33. */
  34.  
  35. #include <Dialogs.h>
  36. #include <Fonts.h>    
  37.  
  38. #define    kNumSteps        14
  39. #define    kRectsVisible    4
  40. #define    kZoomRatio        .7
  41. #define    kDelayTicks        1
  42.  
  43. #define    kZoomLarger        true        // change this to zoom "inward"
  44.  
  45.  
  46. void InitStuff(void);
  47. void ZoomRect(Boolean zoomOut,Rect *smallRect, Rect *bigRect);
  48. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue);
  49. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo);
  50. void FixRect(Rect *theRect,Rect *rightRect);
  51.  
  52. Boolean gDone;
  53.  
  54. void main(void)
  55. {
  56.     WindowPtr window;
  57.     Rect bounds = {44,12,330,500},zoomFrom,zoomTo;
  58.         
  59.     InitStuff();
  60.     window = NewWindow(nil,&bounds,"\pDrag Two Rects to Zoom",true,documentProc,(WindowPtr)-1L,true,0);
  61.     SetPort(window);
  62.     
  63.     do {
  64.         if (GetRects(&zoomFrom,&zoomTo))
  65.             ZoomRect(kZoomLarger,&zoomFrom,&zoomTo);
  66.         EraseRect(&window->portRect);
  67.     }
  68.     while (!gDone);
  69.     
  70.     FlushEvents(everyEvent,0);
  71. }
  72.  
  73.     
  74. void InitStuff(void)
  75. {
  76.     InitGraf(&qd.thePort);
  77.     InitFonts();
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs(nil);
  82.     InitCursor();
  83.     FlushEvents(everyEvent,0);
  84. }
  85.  
  86.  
  87. void ZoomRect(Boolean zoomLarger,Rect *smallRect, Rect *bigRect)
  88. {
  89.     double firstStep,stepValue,trailer,zoomRatio;
  90.     short i,step;
  91.     Rect curRect;
  92.     unsigned long ticks;
  93.     
  94.     PenPat(&qd.gray);
  95.     PenMode(patXor);
  96.     
  97.     
  98.     firstStep=kZoomRatio;
  99.     for (i=0; i<kNumSteps; i++) {
  100.         firstStep *= kZoomRatio;
  101.     }
  102.  
  103.     if (!zoomLarger) {
  104.         zoomRatio = 1.0/kZoomRatio;
  105.         firstStep = 1.0-firstStep;
  106.     }
  107.     else
  108.         zoomRatio = kZoomRatio;
  109.         
  110.     trailer = firstStep;
  111.     stepValue = firstStep;
  112.     for (step=0; step<(kNumSteps+kRectsVisible); step++) {
  113.     
  114.         // draw new frame
  115.         
  116.         if (step<kNumSteps) {
  117.             stepValue /= zoomRatio;
  118.             CalcRect(&curRect,smallRect,bigRect,stepValue);
  119.             FrameRect(&curRect);
  120.         }
  121.         
  122.         // erase old frame
  123.         
  124.         if (step>=kRectsVisible) {
  125.             trailer /= zoomRatio;
  126.             CalcRect(&curRect,smallRect,bigRect,trailer);
  127.             FrameRect(&curRect);
  128.         }
  129.  
  130.         Delay(kDelayTicks,&ticks);
  131.     }
  132.  
  133.     PenNormal();
  134. }
  135.  
  136.  
  137. void CalcRect(Rect *theRect,Rect *smallRect,Rect *bigRect,double stepValue)
  138. {
  139.     theRect->left = smallRect->left + (short)((double)(bigRect->left-smallRect->left)*stepValue);
  140.     theRect->top = smallRect->top + (short)((double)(bigRect->top-smallRect->top)*stepValue);
  141.     theRect->right = smallRect->right + (short)((double)(bigRect->right-smallRect->right)*stepValue);
  142.     theRect->bottom = smallRect->bottom + (short)((double)(bigRect->bottom-smallRect->bottom)*stepValue);
  143. }
  144.  
  145.  
  146. Boolean GetRects(Rect *zoomFrom,Rect *zoomTo)
  147. {
  148.     short numRects = 0;
  149.     /*EventRecord ev;*/
  150.     Rect theRect,drawRect;
  151.     Point firstPt,curPt,oldPt,globalPt;
  152.     /*KeyMap theKeys;*/
  153.     WindowPtr window;
  154.     Boolean result = true;
  155.     
  156.     PenMode(patXor);
  157.     
  158.     do {
  159.         while (!Button()){
  160.         
  161.         GetMouse(&globalPt);
  162.         LocalToGlobal(&globalPt);
  163.         if ((FindWindow(globalPt,&window)==inGoAway) && window==FrontWindow()) {
  164.             gDone = true;
  165.             result = false;
  166.         }
  167.         
  168.         GetMouse(&firstPt);
  169.         oldPt = firstPt;
  170.         SetRect(&theRect,firstPt.h,firstPt.v,firstPt.h,firstPt.v);
  171.         }
  172.         while (Button()) {
  173.             GetMouse(&curPt);
  174.             if (!EqualPt(curPt,oldPt)) {
  175.                 FixRect(&theRect,&drawRect);
  176.                 FrameRect(&drawRect);
  177.                 oldPt = curPt;
  178.                 theRect.right = curPt.h;
  179.                 theRect.bottom = curPt.v;
  180.                 FixRect(&theRect,&drawRect);
  181.                 FrameRect(&drawRect);
  182.             }
  183.         }
  184.         
  185.         FixRect(&theRect,&drawRect);
  186.         if (numRects==0)
  187.             *zoomFrom = drawRect;
  188.         else
  189.             *zoomTo = drawRect;
  190.             
  191.         numRects++;
  192.  
  193.     } while (numRects<2);
  194.     
  195.     PenNormal();
  196.     
  197.     return result;
  198. }
  199.  
  200.  
  201. void FixRect(Rect *theRect,Rect *rightRect)
  202. {
  203.     if (theRect->right > theRect->left) {
  204.         rightRect->right = theRect->right;
  205.         rightRect->left = theRect->left;
  206.     }
  207.     else {
  208.         rightRect->right = theRect->left;
  209.         rightRect->left = theRect->right;
  210.     }
  211.     
  212.     if (theRect->bottom > theRect->top) {
  213.         rightRect->bottom = theRect->bottom;
  214.         rightRect->top = theRect->top;
  215.     }
  216.     else {
  217.         rightRect->bottom = theRect->top;
  218.         rightRect->top = theRect->bottom;
  219.     }
  220.     
  221. }
  222.